package webjoch.process.strategies.bosses;

import com.rsbuddy.script.methods.Npcs;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.wrappers.Area;
import com.rsbuddy.script.wrappers.GameObject;
import com.rsbuddy.script.wrappers.Npc;
import com.rsbuddy.script.wrappers.Tile;

import webjoch.process.strategies.Boss;

public class GluttonousBehemoth extends Boss {
    public final int[] BOSS_ID = {9950, 9951};
    public final int[] BOSS_FOOD_ID = {49283};
    
    /*
     * Recommened strategy:
     * 
     * location   - Stand between BOSS_FOOD_ID and BOSS_ID
     * prayer     - Protect_From_Magic
     * fight mode - slash / crush
     */
    
    @Override
    public boolean isValid() {
    	System.out.println(room.getLocation());
        return room.getObject(BOSS_FOOD_ID) != null;
    }

    @Override
    public boolean isFinished() {
        return room.getObject(STRANGE_WALL_OPEN) != null;
    }
    
    @Override
    public BossType getBossType() {
        return BossType.GLUTTONOUS_BEHEMOTH;
    }

    @Override
    public int loop() {
        Npc boss = Npcs.getNearest(BOSS_ID);
        Tile loc = getRequiredLocation();
        if (room.getArea().contains(Players.getLocal())) {
            System.out.println("Preffered loc: " + loc.getX() + "," + loc.getY());
            if (Players.getLocal().getLocation().equals(loc)) {
                if (Players.getLocal().getInteracting() != boss) {
                    boss.interact("Attack");
                    return 500;
                } else {
                    return 50;
                }
            } else {
                if (loc.isOnScreen()) {
                    loc.interact("Walk");
                } else {
                    loc.clickOnMap();
                }
                return 500;
            }
        }
        return 0;
    }
    
    
    public Tile getRequiredLocation() {
      // TODO check if this method actually actually works.
      Npc boss = room.getNpc(BOSS_ID);
        GameObject food = room.getObject(BOSS_FOOD_ID);
        if (boss == null || food == null) return Players.getLocal().getLocation();
        
        Area bossArea = boss.getArea();
        Area foodArea = food.getArea();
        //Expands the bounds of the foodArea by 1 in each direction
        Area foodBoundsArea = new Area(foodArea.getX() - 1, foodArea.getY() - 1, foodArea.getX() + foodArea.getWidth() + 2, foodArea.getY() + foodArea.getHeight() + 2);
       
        Tile recommened = foodBoundsArea.getNearestTile(bossArea.getCentralTile());
        //Failsafe of somehow wrong tile is selected
        if (foodBoundsArea.contains(recommened) && !foodArea.contains(recommened)) {
            return recommened;
        }
        return Players.getLocal().getLocation();
    }


}
